package com.smartleanx.module.hrm.controller.admin.examine.examineResultConfig;

import com.alibaba.fastjson2.JSONObject;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.framework.excel.core.util.ExcelUtils;

import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineResultConfig.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineResultConfig.ExamineResultConfigDO;
import com.smartleanx.module.hrm.service.examine.examineResultConfig.ExamineResultConfigService;

@Tag(name = "管理后台 - 考核结果设置项	")
@RestController
@RequestMapping("/hrm/examineresultconfig")
@Validated
public class ExamineResultConfigController {

    @Resource
    private ExamineResultConfigService examineResultConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建考核结果设置项	")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result-config:create')")
    public CommonResult<Integer> createExamineResultConfig(@Valid @RequestBody ExamineResultConfigSaveReqVO createReqVO) {
        return success(examineResultConfigService.createExamineResultConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考核结果设置项	")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result-config:update')")
    public CommonResult<Boolean> updateExamineResultConfig(@Valid @RequestBody ExamineResultConfigSaveReqVO updateReqVO) {
        examineResultConfigService.updateExamineResultConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考核结果设置项	")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:examine-result-config:delete')")
    public CommonResult<Boolean> deleteExamineResultConfig(@RequestParam("id") Integer id) {
        examineResultConfigService.deleteExamineResultConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考核结果设置项	")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result-config:query')")
    public CommonResult<ExamineResultConfigRespVO> getExamineResultConfig(@RequestParam("id") Integer id) {
        ExamineResultConfigDO examineResultConfig = examineResultConfigService.getExamineResultConfig(id);
        return success(BeanUtils.toBean(examineResultConfig, ExamineResultConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考核结果设置项	分页")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result-config:query')")
    public CommonResult<PageResult<ExamineResultConfigRespVO>> getExamineResultConfigPage(@Valid ExamineResultConfigPageReqVO pageReqVO) {
        PageResult<ExamineResultConfigDO> pageResult = examineResultConfigService.getExamineResultConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamineResultConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考核结果设置项	 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result-config:export')")
    @OperateLog(type = EXPORT)
    public void exportExamineResultConfigExcel(@Valid ExamineResultConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamineResultConfigDO> list = examineResultConfigService.getExamineResultConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考核结果设置项	.xlsx", "数据", ExamineResultConfigRespVO.class,
                        BeanUtils.toBean(list, ExamineResultConfigRespVO.class));
    }


    @GetMapping("/selectTableList")
    public CommonResult<Object> selectTableList(Integer examineId){
        return CommonResult.success(examineResultConfigService.selectTableList(examineId));
    }

}